package com.demo.Test;

import java.sql.*;
import java.util.ArrayList;

public class Test3 {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url= "jdbc:mysql://106.13.7.203:3306/wg12?useCursorFetch=true&amp;autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=UTF-8";
        String username = "root";
        String password = "Asd6250390@";
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection(url, username, password);
        System.out.println("连接成功");
        Statement st = conn.createStatement();
        String sql2 = "select id from users";
        ResultSet rs= st.executeQuery(sql2);
        ArrayList al = new ArrayList();
        while (rs.next()){
            int id = rs.getInt("id");
            al.add(id);
        }
        System.out.println(al);
        if(!al.contains(5)) {
            String sql = "insert into users(id, name, gender, age) values(5, 'HiMei', '女', 20)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.execute();
        }else {
            System.out.println("id已存在！");
        }
    }
}
